<?php

class OrderSentController extends BaseController
{
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Order::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	
	//待发货订单,发货
	public function actionDelivery()
	{
		
		if(isset($_POST['Order'])){
			$id = trim($_POST['Order']['zo_id']);
			$model = Order::model()->findByPk($id);
			$model->attributes=$_POST['Order'];
			if($model->zo_exp_status==1){
				$model->zo_exp_status = 2;//更新配送状态
			}elseif($model->zo_exp_status==4){
				$model->zo_exp_status = 3;//更新配送状态
			}else{
				throw new CHttpException(404,'The requested page does not exist.');
			}
			$model->zo_send_time = time();
			if($model->save()){
				$order = Order::model()->findByPk($id);
				//交易提醒:发货提醒
				OrdMsg::model()->setOrdMsg($order->zm_id,$order->zo_id,3);
				
				//如果是支付宝担保交易支付，加载支付宝发货操作
				// if($order->zpy_id ==4){
				// 	//实例化支付类
				// 	$alipay = new $order->payment->class_name();
				// 	//配置
				// 	$alipay->pay_config = $order->payment->zpy_config;
				// 	$params = array(
				// 			"trade_no"	=> $order->zo_trade_no,//支付宝交易号
				// 			"logistics_name"	=> $order->expressCom->zec_title,//物流公司名称
				// 			"invoice_no"	=> trim($order->zo_express_id), //物流发货单号
				// 			"transport_type"	=> 'EXPRESS',//物流类型，三个值可选：EXPRESS（快递）、POST（平邮）、EMS（EMS）
				// 	);
				// 	$alipay->setGoods($params);
				// }
				
				//成功跳转
				$this->render('send_success',array('zo_express_id'=>$model->zo_express_id,'zo_express_com'=>$model->zo_express_com));
				exit;
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，发货失败，请稍后再试！'));
				exit;
			}
		}
		if(isset($_GET['id'])){
			$id = trim($_GET['id']);
			$model = Order::model()->findByPk($id);
			//如果订单不为待发货状态，待换货状态，不能作发货操作
			if($model->zo_exp_status!=1&&$model->zo_exp_status!=4){
				//订单不为发货状态，跳到错误页面
				$this->render('../index/error',array('message'=>'非法操作，订单已发货！'));
				exit;
			}
			$this->render('send_good',array('model'=>$model));
			exit;
		}
		$model = new Order();
		$model->unsetAttributes();
		//取头前商家ID
		$model->cmpUserId = Company::getCmpUserId();
		$this->render('delivery',array(
				'model'=>$model,
		));
	}


}
